﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
using System.ServiceModel;
using System.ServiceModel.Channels;

namespace WEI_Share.Helpers
{
    public sealed class Globals
    {
		#region Constructor
        /// <summary>
        /// Private constructor
        /// </summary>
		private Globals()
        {
        }
        #endregion

		/// <summary>
		/// Used to determine in which position the submittal completed
		/// </summary>
		public enum WinsatSubmittalPosition
		{
			Completed,
			Graphics,
			HardDiskSpeed,
			Monitor,
			NotStarted,
			OS,
			PhysicalMemory,
			Platform,
			PrimaryHardDiskFreeSpace,
			Processor,
			ProcessorL1Cache,
			ProcessorL2Cache,
			SystemScores,
			SystemNetworkCard,
			SystemPhysicalMemory,
			SystemPrimaryHardDiskFreeSpace,
			SystemSystemInformation,
			TouchPoints,
			WCFServiceInitial,
			WCFServiceChannelCreate
		}


		/// <summary>
		/// Key used for web service comunication to validae communication
		/// </summary>
		public static string ServiceKey
        {
            get
            {
                return String.Format(Properties.Resources.WEIServiceKey + "|{0}", DateTime.UtcNow);
            }
        }


		/// <summary>
		/// Gets/sets the file path and name of the WinSat file to be used for processing
		/// </summary>
        public static string WinSatFileName { get; set; }

		/// <summary>
		/// Gets the path and fielname of the winsat executable
		/// </summary>
		public static string WinSatDataStoreFolderPath
		{
			get
			{
				//use SystemRoot or windir
				string folderPath = Environment.GetEnvironmentVariable("SystemRoot") + @"\Performance\WinSAT\DataStore";


				return folderPath;
			}
		}
        
		/// <summary>
		/// The experience index xml document
		/// </summary>
		public static XElement WinSatXmlDocument { get; set; }


		/// <summary>
		/// The URL of the WEI info display site
		/// </summary>
        public static string Website
        {
			get { return Properties.Resources.WEIWebsite; }
        }




		/// <summary>
		/// The URL of the WEI score images for facebook
		/// </summary>
		public static string FacebookScoreImageFolder
		{
			get { return Properties.Resources.WEIFacebookImageFolderUrl; }
		}

		/// <summary>
		/// Gets the path and fielname of the winsat executable
		/// </summary>
		public static string WinSatExecutable
		{
			get
			{
				return Environment.GetFolderPath(Environment.SpecialFolder.System) + @"\WinSat.exe";
			}
		}

		/// <summary>
		/// Gets the parameters to be added to the executable when run
		/// </summary>
		public static string WinSatExecutableParameters
		{
			get
			{
				return "formal";
			}
		}

        /// <summary>
        /// The binary encoding objct for the custom binding
        /// </summary>
        public static BinaryMessageEncodingBindingElement CustomServiceBinding
        {
            get
            {
                BinaryMessageEncodingBindingElement binaryEncoding = new BinaryMessageEncodingBindingElement();
                binaryEncoding.MaxReadPoolSize = 64;
                binaryEncoding.MaxSessionSize = 2048;
                binaryEncoding.MaxWritePoolSize = 16;
                binaryEncoding.ReaderQuotas.MaxDepth = 32;
                binaryEncoding.ReaderQuotas.MaxStringContentLength = 8192;
                binaryEncoding.ReaderQuotas.MaxArrayLength = 16384;
                binaryEncoding.ReaderQuotas.MaxBytesPerRead = 4096;
                binaryEncoding.ReaderQuotas.MaxNameTableCharCount = 16384;

                return binaryEncoding;
            }
        }

        /// <summary>
        /// The transport element for the custom binding
        /// </summary>
        public static HttpTransportBindingElement CustomTransportBinding
        {
            get
            {
                HttpTransportBindingElement transportBinding = new HttpTransportBindingElement();
                transportBinding.ManualAddressing = false;
                transportBinding.MaxBufferPoolSize = 524288;
                transportBinding.MaxReceivedMessageSize = 65536;
                transportBinding.AllowCookies = false;
                transportBinding.AuthenticationScheme = System.Net.AuthenticationSchemes.Anonymous;
                transportBinding.BypassProxyOnLocal = false;
				//decompressionEnabled="true" 
                transportBinding.HostNameComparisonMode = HostNameComparisonMode.StrongWildcard;
                transportBinding.KeepAliveEnabled = true;
                transportBinding.MaxBufferSize = 65536;
                transportBinding.ProxyAuthenticationScheme = System.Net.AuthenticationSchemes.Anonymous;
                transportBinding.Realm = string.Empty;
                transportBinding.TransferMode = TransferMode.Buffered;
                transportBinding.UnsafeConnectionNtlmAuthentication = false;
                transportBinding.UseDefaultWebProxy = true;

                return transportBinding;
            }
        }

		/// <summary>
		/// the endpoint used for the service communication
		/// </summary>
        public static EndpointAddress CustomEndPoint
        {
            get
            {
				return new EndpointAddress(Properties.Resources.WEIServiceEndpoint);

            }
		}


		#region GetWinsatSubmittalErrorMessage
		/// <summary>
		/// Convertsthe last positon enumeration to a specificerror message
		/// </summary>
		/// <param name="currentPosition"></param>
		/// <returns></returns>
		public static string GetWinsatSubmittalErrorMessage(Globals.WinsatSubmittalPosition currentPosition, Exception ex)
		{
			string messagePrefix = "Unable to retrieve ";
			string systemSuffix = " information from the Windows Experience Index.";
			string winsatSuffix = " information from the system.";
			bool fromWinsat = true;

			string currentItem = null;

			switch (currentPosition)
			{
				case Globals.WinsatSubmittalPosition.Graphics:
					currentItem = "Graphics";
					break;
				case Globals.WinsatSubmittalPosition.HardDiskSpeed:
					currentItem = "Hard Disk read/write";
					break;
				case Globals.WinsatSubmittalPosition.Monitor:
					currentItem = "Monitor";
					break;
				case Globals.WinsatSubmittalPosition.OS:
					currentItem = "OS";
					break;
				case Globals.WinsatSubmittalPosition.PhysicalMemory:
					currentItem = "Physical Memory";
					break;
				case Globals.WinsatSubmittalPosition.Platform:
					currentItem = "Platform";
					break;
				case Globals.WinsatSubmittalPosition.PrimaryHardDiskFreeSpace:
					currentItem = "Primary Hard Disk free space";
					break;
				case Globals.WinsatSubmittalPosition.Processor:
					currentItem = "Processor";
					break;
				case Globals.WinsatSubmittalPosition.ProcessorL1Cache:
					currentItem = "Processor L1 Cache";
					break;
				case Globals.WinsatSubmittalPosition.ProcessorL2Cache:
					currentItem = "Processor L2 Cache";
					break;
				case Globals.WinsatSubmittalPosition.SystemScores:
					currentItem = "System Score";
					break;
				case Globals.WinsatSubmittalPosition.SystemNetworkCard:
					currentItem = "Network Card";
					fromWinsat = false;
					break;
				case Globals.WinsatSubmittalPosition.SystemPhysicalMemory:
					currentItem = "Physical Memory";
					fromWinsat = false;
					break;
				case Globals.WinsatSubmittalPosition.SystemPrimaryHardDiskFreeSpace:
					currentItem = "Primary Hard Disk free space";
					fromWinsat = false;
					break;
				case Globals.WinsatSubmittalPosition.SystemSystemInformation:
					currentItem = "basic System";
					break;
			}

			if (!string.IsNullOrEmpty(currentItem))
				return messagePrefix + currentItem + (fromWinsat ? systemSuffix : winsatSuffix);
			else if (currentPosition == Globals.WinsatSubmittalPosition.WCFServiceInitial)
				return "Unable to create bindings or channel connection to WCF Service.";
			else if (currentPosition == Globals.WinsatSubmittalPosition.WCFServiceChannelCreate)
				return String.Format("Unable to post data to WCF Service. {0}", ex.Message);

			return "";
		}
		#endregion
    }
}
